/**
 *
 * MobileLib   
 * MyLib.java
 * Dec 29, 2011
 * @author qidiven
 *
 */
package com.ustc.mobilelib.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;

import com.ustc.diven.model.R;
import com.ustc.mobilelib.model.DataCenter;

public class MyLibActivity extends Activity{
	
	private static final String TAG = MyLibActivity.class.toString();
	private ImageButton reading = null;
	private ImageButton readed = null;
	private ImageButton myInfo = null;
	private ImageButton youmayLike = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mylib);
		init();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK)
		{
			logoutDialog();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void logoutDialog() {
		AlertDialog.Builder builder = new Builder(MyLibActivity.this);
//		builder.setMessage(getString(R.string.logout_confirm));
		builder.setIcon(R.drawable.logo);
		builder.setTitle(getString(R.string.logout_confirm));
		builder.setCancelable(false);
		
		builder.setPositiveButton(getString(R.string.confirm), new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
//				MyLibActivity.this.finish();
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
		
		builder.setNegativeButton(getString(R.string.cancel), new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	
	private void init()
	{
		this.getWindow().setBackgroundDrawableResource(R.color.loading_bg);
		reading = (ImageButton) findViewById(R.id.reading);
		readed = (ImageButton) findViewById(R.id.readed);
		myInfo = (ImageButton) findViewById(R.id.myinfo);
		youmayLike = (ImageButton) findViewById(R.id.mayLike);
		
		reading.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!DataCenter.getInstance().isHasLogin())
				{
					Intent intent = new Intent(MyLibActivity.this, LoginActivity.class);
					startActivity(intent);
				}else
				{
					Intent intent = new Intent(MyLibActivity.this, ReadingActivity.class);
					startActivity(intent);
				}
			}
		});
		
		reading.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if(MotionEvent.ACTION_DOWN == arg1.getAction())
				{
					reading.setImageResource(R.drawable.reading_press);
				}
				else if(MotionEvent.ACTION_UP == arg1.getAction())
				{
					reading.setImageResource(R.drawable.reading);
				}
				return false;
			}
		});
		
		readed.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!DataCenter.getInstance().isHasLogin())
				{
					Intent intent = new Intent(MyLibActivity.this, LoginActivity.class);
					startActivity(intent);
				}else
				{
					Intent intent = new Intent(MyLibActivity.this, ReadedActivity.class);
					startActivity(intent);
				}
			}
		});
		
		readed.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if(MotionEvent.ACTION_DOWN == arg1.getAction())
				{
					readed.setImageResource(R.drawable.readed_press);
				}
				else if(MotionEvent.ACTION_UP == arg1.getAction())
				{
					readed.setImageResource(R.drawable.readed);
				}
				return false;
			}
		});
		
		myInfo.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!DataCenter.getInstance().isHasLogin())
				{
					Intent intent = new Intent(MyLibActivity.this, LoginActivity.class);
					startActivity(intent);
				}else
				{
					Intent intent = new Intent(MyLibActivity.this, MyInfoActivity.class);
					startActivity(intent);
				}
			}
		});
		
		myInfo.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if(MotionEvent.ACTION_DOWN == arg1.getAction())
				{
					myInfo.setImageResource(R.drawable.myinfo_press);
				}
				else if(MotionEvent.ACTION_UP == arg1.getAction())
				{
					myInfo.setImageResource(R.drawable.myinfo);
				}
				return false;
			}
		});
		
		youmayLike.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!DataCenter.getInstance().isHasLogin())
				{
					Intent intent = new Intent(MyLibActivity.this, LoginActivity.class);
					startActivity(intent);
				}else
				{
					Intent intent = new Intent(MyLibActivity.this, IntroductionActivity.class);
					startActivity(intent);
				}
			}
		});
		
		youmayLike.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if(MotionEvent.ACTION_DOWN == arg1.getAction())
				{
					youmayLike.setImageResource(R.drawable.youmaylike_press);
				}
				else if(MotionEvent.ACTION_UP == arg1.getAction())
				{
					youmayLike.setImageResource(R.drawable.youmaylike);
				}
				return false;
			}
		});
		
		
	}
	
}
